package com.yb.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yb.project.base.mapper.BaseMapper;
import com.yb.project.base.service.impl.BaseServiceImpl;
import com.yb.project.system.domain.SysRole;
import com.yb.project.system.domain.SysRoleMenu;
import com.yb.project.system.mapper.SysRoleMapper;
import com.yb.project.system.service.SysRoleMenuService;
import com.yb.project.system.service.SysRoleService;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	protected BaseMapper<SysRole> getMapper() {
		return sysRoleMapper;
	}
	
	@Override
	public void setMenu(Long roleId, Long[] menuIds) {
		// TODO Auto-generated method stub
		// 先删除
		sysRoleMenuService.deleteByRoleId(roleId);
		
		// 再新增
		if(menuIds != null && menuIds.length > 0) {
			List<SysRoleMenu> sysRoleMenuList = new ArrayList<SysRoleMenu>();
			
			for (Long menuId : menuIds) {
				SysRoleMenu sysRoleMenu = new SysRoleMenu(roleId, menuId);
				sysRoleMenuList.add(sysRoleMenu);
			}
			sysRoleMenuService.insertList(sysRoleMenuList);
		}
	}
}